package com.fire.core.util;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;



/**
 *类描述：
 *@author: zengzhengfu
 *@date： 日期：2018年4月30日 时间：下午2:43:20
 *@version 1.0
 */
public class BlogUtil {
	private static final String WHO = "who";
	private static final String WHEN = "when";
	private static final String WHERE = "where";
	private static final String WHAT = "what";
	private static final String TYPE = "type";
	private static final String ATTACH = "attach";
    private static final Logger blogin = LoggerFactory.getLogger("blogin");
    private static final Logger blogout = LoggerFactory.getLogger("blogout");
	/**
	 * inlog输出内部日志，即与前端交互的日志
	 * @param json 前端传入的Json字符串或者返回给前端的Json字符串
	 * @param who 有userid则传userid
	 * @param where 请求路径
	 * @param what  请求接口
	 * @param type 收到请求对应"req",发送响应对应"resp"
	 */
    public static void inlog(String json,String who,String where,String what,String type){
		JSONObject content = JSONObject.parseObject(json);
		Map<String,Object> map = new LinkedHashMap<String,Object>();
		String date = DateUtil.getTime();
		map.put(WHO,who);
		map.put(WHEN,date);
		map.put(WHERE,where);
		map.put(WHAT,what);
		map.put(TYPE,type);
		map.put("content", content);
		String log = JSONObject.toJSONString(map);
		log = log.replaceAll("\\\\","");
		blogin.info(log);
	}
    /**
     * outlog输出外部日志，即与金科交互的日志
     * @param json 请求金科所包含的Json字符串或者金科响应返回的Json字符串，即报文中content标签内的Json数据
     * @param who 有userid则传userid
     * @param where 请求路径
     * @param what  请求接口
     * @param type 发送请求对应"req",收到响应对应"resp"
     * @param attach 订单关联订单号，商品关联商品id
     */
    public static void outlog(String json,String who,String where,String what,String type,String attach){
    	JSONObject content = JSONObject.parseObject(json);
		Map<String,Object> map = new LinkedHashMap<String,Object>();
		String date = DateUtil.getTime();
		map.put(WHO,who);
		map.put(WHEN,date);
		map.put(WHERE,where);
		map.put(WHAT,what);
		map.put(TYPE,type);
		map.put(ATTACH,attach);
		map.put("content", content);
		String log = JSONObject.toJSONString(map);
		log=log.replaceAll("\\\\","");
    	blogout.info(log);
    }


	
	public static void main(String[] args) {
		//模拟请求Json
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("userid", "123423");
		map.put("channelid", "987497");
		map.put("goodsid", "65487678");
		String json = JSONObject.toJSONString(map);
		String userid = "123456";
		
		//输出内部请求日志
		inlog(json, userid, "/mit/user/preapply", "preapply", "req");
		
		//业务处理......返回AjaxResult
		AjaxResult ajaxResult = new AjaxResult();
		ajaxResult.setStatus("0");
		ajaxResult.setMessage("操作成功");
		ajaxResult.setTips("操作成功");
		
		//输出内部响应日志
		String ajaxResJson = JSONObject.toJSONString(ajaxResult);
		inlog(ajaxResJson, userid, "/mit/user/preapply", "preapply", "resp");
	}
	
}
